/**
 * 
 */
package com.pos.modules;

import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.util.Log;
import android.view.KeyEvent;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;

import com.pos.BaseActivity;
import com.pos.R;
import com.pos.widgets.ScanGunKeyEventHelper;

/**
 * @description:测试界面
 * @author cai
 * @time:2017年10月21日下午3:55:42
 */
public class TestActiity extends BaseActivity implements ScanGunKeyEventHelper.OnScanSuccessListener {
	private EditText edit_user;
	private ScanGunKeyEventHelper mScanGunKeyEventHelper;

	@Override
	public void initView() {
		setContentView(R.layout.test_layout);
		edit_user = bindId(R.id.edit_user);
		edit_user.clearFocus();
	}

	@Override
	public void initData() {
		mScanGunKeyEventHelper = new ScanGunKeyEventHelper(this);
		registerBoradcastReceiver();

	}

	@Override
	public void initListener() {
		// TODO Auto-generated method stub

	}

	/**
	 * 截获按键事件.发给ScanGunKeyEventHelper
	 *
	 * @param event
	 * @return
	 */
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {

		if (mScanGunKeyEventHelper.isScanGunEvent(event)) {
			mScanGunKeyEventHelper.analysisKeyEvent(event);
			return true;
		}
		return super.dispatchKeyEvent(event);
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (!mScanGunKeyEventHelper.hasScanGun()) {
			Toast.makeText(mContext, "未检测到扫码枪设备", Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(stateChangeReceiver);
		mScanGunKeyEventHelper.onDestroy();
	}

	@Override
	public void onScanSuccess(String barcode) {
		Log.e(Tag, "onScanSuccess barcode=" + barcode);
		edit_user.setText(barcode);
		InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(edit_user.getWindowToken(), 0);

	}

	private void registerBoradcastReceiver() {
		IntentFilter filter1 = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
		IntentFilter filter2 = new IntentFilter(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
		registerReceiver(stateChangeReceiver, filter1);
		registerReceiver(stateChangeReceiver, filter2);
	}

	private BroadcastReceiver stateChangeReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();

			if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
				Toast.makeText(mContext, "蓝牙设备连接状态已变更", Toast.LENGTH_SHORT).show();
			} else if (action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {
				Toast.makeText(mContext, "蓝牙设备连接状态已变更", Toast.LENGTH_SHORT).show();
			}
		}
	};

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
	}

}
